package com.lzb.yabbs.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.TimeUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.lzb.yabbs.AppConfig;
import com.lzb.yabbs.R;
import com.lzb.yabbs.dao.Comment;
import com.lzb.yabbs.util.TimeManager;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by liuzaibing on 2015/9/8.
 */
public class CardInfoAdapter extends BaseAdapter {

    private Context mContext;
    private List<Comment> mComments = new ArrayList<>();
    private DisplayImageOptions options;

    public CardInfoAdapter(Context mContext, List<Comment> mComments) {
        this.mContext = mContext;
        this.mComments = mComments;

        options = new DisplayImageOptions.Builder().showImageOnLoading(mContext.getResources().getDrawable(R.mipmap.ic_launcher))
                .showImageOnFail(mContext.getResources().getDrawable(R.mipmap.ic_launcher))
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .build();
    }

    @Override
    public int getCount() {
        return mComments.size();
    }

    @Override
    public Comment getItem(int position) {
        return mComments.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void clearData() {
        mComments.clear();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.commet_item, parent, false);
            viewHolder.avatarIv = (CircleImageView) convertView.findViewById(R.id.avatar_iv);
            viewHolder.commentContentWv = (WebView) convertView.findViewById(R.id.comment_content_wv);
            viewHolder.floorTv = (TextView) convertView.findViewById(R.id.floor_tv);
            viewHolder.timeTv = (TextView) convertView.findViewById(R.id.time_tv);
            viewHolder.usernameTv = (TextView) convertView.findViewById(R.id.username_tv);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.usernameTv.setText(getItem(position).getUsername());
        viewHolder.timeTv.setText(TimeManager.getTimeToNow(getItem(position).getComment_time()));
        viewHolder.floorTv.setText("第" + (position + 1) + "楼");
        viewHolder.commentContentWv.loadDataWithBaseURL(null, getItem(position).getComment_content(), AppConfig.mimeType, AppConfig.encoding, null);

        viewHolder.commentContentWv.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                viewHolder.commentContentWv.requestLayout();
                MyRunnable myRunnable = new MyRunnable(viewHolder.commentContentWv);
                Thread thread = new Thread(myRunnable);

            }
        });
        String imgUrl = getItem(position).getAvatar();
        if (null == imgUrl || imgUrl.equals("") || imgUrl.equals("null"))
            ImageLoader.getInstance().displayImage(AppConfig.MO_REN_avatar, viewHolder.avatarIv, options);
        else
            ImageLoader.getInstance().displayImage(getItem(position).getAvatar(), viewHolder.avatarIv, options);
        return convertView;
    }

    class ViewHolder {
        CircleImageView avatarIv;
        TextView usernameTv;
        TextView timeTv;
        TextView floorTv;
        WebView commentContentWv;
    }

    class MyRunnable implements Runnable {
        private WebView webView;

        MyRunnable(WebView webView) {
            this.webView = webView;
        }

        @Override
        public void run() {
            try {
                Thread.sleep(15000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            ((Activity) mContext).runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    webView.requestLayout();
                }
            });

        }
    }
}
